package com.ymm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ymm.domain.entity.Role;
import com.ymm.domain.entity.User;
import com.ymm.domain.entity.UserRole;
import com.ymm.domain.vo.PageVo;
import com.ymm.domain.vo.ResponseResult;
import com.ymm.domain.vo.UserListVo;
import com.ymm.enums.AppHttpCodeEnum;
import com.ymm.enums.SystemContains;
import com.ymm.exception.SystemException;
import com.ymm.mapper.UserMapper;
import com.ymm.service.RoleService;
import com.ymm.service.UserRoleService;
import com.ymm.service.UserService;
import com.ymm.utils.BeanCopyUtils;
import com.ymm.utils.email.EmailValidator;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;

/**
 * (User)表服务实现类
 *
 * @author 椰咩咩咩
 * @since 2023-03-30 23:07:53
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private RoleService roleService;

    @Override
    public ResponseResult pageUsers(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), User::getName, name);
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<User> users = page.getRecords();
        List<UserListVo> userListVos = BeanCopyUtils.copyBeanList(users, UserListVo.class);
        return ResponseResult.okResult(new PageVo(userListVos, page.getTotal()));
    }

    @Override
    public ResponseResult addUsers(User user) {
        if (!StringUtils.hasText(user.getUserName())) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        } else if (!StringUtils.hasText(user.getPassword())) {
            throw new SystemException(AppHttpCodeEnum.REQUIRE_PASSWORD);
        } else if (!StringUtils.hasText(user.getEmail()) || !EmailValidator.isValidEmail(user.getEmail())) {
            throw new SystemException(AppHttpCodeEnum.INVALID_EMAIL);
        }
        user.setName("用户_" + user.getUserName());
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        save(user);
        // 赋予用户权限
        UserRole userRole = new UserRole();
        userRole.setUserId(user.getId());
        if (user.getType().equals(SystemContains.USER_ADMIN)) {
            userRole.setRoleId(SystemContains.BASE_ROLE_ADMIN);
        } else if (user.getType().equals(SystemContains.USER_DOCTOR)) {
            userRole.setRoleId(SystemContains.BASE_ROLE_DOCTOR);
        } else {
            userRole.setRoleId(SystemContains.BASE_ROLE_STUDENT);
        }
        userRoleService.save(userRole);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult modifyUser(User user) {
        updateById(user);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getStudentList(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), User::getName, name);
        wrapper.eq(User::getType, SystemContains.USER_STUDENT);
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<User> users = page.getRecords();
        List<UserListVo> userListVos = BeanCopyUtils.copyBeanList(users, UserListVo.class);
        return ResponseResult.okResult(new PageVo(userListVos, page.getTotal()));
    }

}

